#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QMutex>
#include <QDebug>
#include <QStringList>
#include <functional>

enum LogLevel {
    Debug,
    Info,
    Warning,
    Error,
    Fatal
};

// 定义日志回调函数类型
typedef std::function<void(QtMsgType, const QString&)> LogCallback;

class Logger : public QObject
{
    Q_OBJECT

public:
    static Logger* getInstance();
    void log(LogLevel level, const QString &message);
    void setLogFile(const QString &filePath);
    void setLogLevel(LogLevel level);
    
    // 设置日志回调
    void setCallback(const LogCallback &callback);
    
    // 获取最新的日志条目
    QStringList getLatestEntries();

private:
    explicit Logger(QObject *parent = nullptr);
    ~Logger();
    
    static Logger* instance;
    static QMutex mutex;
    
    QFile logFile;
    QTextStream logStream;
    LogLevel currentLevel;
    LogCallback callback;
    
    QStringList logEntries;  // 存储最近的日志条目
    int maxLogEntries;       // 保存的最大日志条目数量
    
    QString levelToString(LogLevel level);
};

// 便捷宏，方便使用
#define LOG_DEBUG(msg) Logger::getInstance()->log(LogLevel::Debug, msg)
#define LOG_INFO(msg) Logger::getInstance()->log(LogLevel::Info, msg)
#define LOG_WARNING(msg) Logger::getInstance()->log(LogLevel::Warning, msg)
#define LOG_ERROR(msg) Logger::getInstance()->log(LogLevel::Error, msg)
#define LOG_FATAL(msg) Logger::getInstance()->log(LogLevel::Fatal, msg)
#define LOG_CALLBACK(cb) Logger::getInstance()->setCallback(cb)

#endif // LOGGER_H 